package wifiunion.com.notice.widget;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import wifiunion.com.notice.R;


/**
 * 通用确认对话框
 *
 * @author dqcao
 */
public class EditDialog extends Dialog {

    private Context context;
    private TextView titleName;
    private TextView cancelTxt;
    private TextView confirmTxt;
    private EditText messgaeEt;


//    public EditDialog(Context context) {
//        super(context, R.style.alert_dialog);
//        this.context = context;
//        LayoutInflater li = LayoutInflater.from(context);
//        ViewGroup contentView = (ViewGroup) li.inflate(
//                R.layout.dialog_edit, null);
//        contentView.bringToFront();
//
//        titleName = (TextView) contentView.findViewById(R.id.tv_title_name);
//        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);
//        confirmTxt = (TextView) contentView.findViewById(R.id.confirm_txt);
//        messgaeEt = (EditText) contentView.findViewById(R.id.tv_message);
//
//        setCanceledOnTouchOutside(false);
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.gravity = Gravity.CENTER;
//        onWindowAttributesChanged(lp);
//        setContentView(contentView);
//
//
//    }


    public EditDialog(Context context, int length) {
        super(context, R.style.alert_dialog);
        this.context = context;
        LayoutInflater li = LayoutInflater.from(context);
        ViewGroup contentView = (ViewGroup) li.inflate(
                R.layout.dialog_edit, null);
        contentView.bringToFront();

        titleName = (TextView) contentView.findViewById(R.id.tv_title_name);
        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);
        confirmTxt = (TextView) contentView.findViewById(R.id.confirm_txt);
        messgaeEt = (EditText) contentView.findViewById(R.id.tv_message);
        messgaeEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        onWindowAttributesChanged(lp);
        setContentView(contentView);


    }

    public String getEditData() {
        String str = null;
        if (null != messgaeEt) {
            str = messgaeEt.getText().toString();
        }
        return str;
    }

    /**
     * 设置默认内容
     * @param str
     */
    public void setEditData(String str){
        if (null != messgaeEt) {
            messgaeEt.setText(str);
            messgaeEt.setSelection(str.length());
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = getContext().getResources().getDimensionPixelSize(
                R.dimen.dp_354);
        lp.gravity = Gravity.CENTER;
        getWindow().setAttributes(lp);
    }

    public void setTitleMsg(String msg) {
        if (titleName != null) {
            titleName.setText(msg);
        }
    }

    public void setCancleText(String text) {
        if (text != null) {
            cancelTxt.setText(text);
        }
    }

    public void setConfirmText(String text) {
        if (text != null) {
            confirmTxt.setText(text);
        }
    }

    public void setMsgHint(String text) {
        if (text != null) {
            messgaeEt.setHint(text);
        }

    }

    public void setComfirmListener(
            android.view.View.OnClickListener okListener) {
        if (confirmTxt != null) {
            confirmTxt.setOnClickListener(okListener);
        }
    }

    public void setCancelListener(android.view.View.OnClickListener okListener) {
        if (cancelTxt != null) {
            cancelTxt.setOnClickListener(okListener);
        }
    }


}
